﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ScaffoldIt.Configuration;
using System.IO;

namespace ScaffoldIt
{
    public partial class FormEditTemplate : Form
    {
        private ScaffoldIt.Configuration.ScaffoldItConfig _cfg;
        public FormEditTemplate()
        {
            InitializeComponent();
        }

        public static bool EditTemplate(ScaffoldIt.Configuration.ScaffoldItConfig cfg, Template category, string[] projects)
        {
            using (var frm = new FormEditTemplate())
            {
                frm.Init(cfg, category, projects);
                frm.textBoxName.Focus();
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    frm.Read(category);
                    return true;
                }
            }

            return false;
        }

        private void Read(Template template)
        {
            template.Name = textBoxName.Text;
            template.Description = textBoxDescription.Text;
            template.OutputName = textBoxOutput.Text;
            template.TemplateName = textBoxTemplate.Text;
            template.ProjectName = comboBoxProjects.Text;
        }

        private void Init(ScaffoldIt.Configuration.ScaffoldItConfig cfg, Template template, string[] projects)
        {
            _cfg = cfg;
            comboBoxProjects.Items.AddRange(projects);
            comboBoxProjects.Text = template.ProjectName;
            textBoxName.Text = template.Name;
            textBoxDescription.Text = template.Description;
            textBoxOutput.Text = template.OutputName;
            textBoxTemplate.Text = template.TemplateName;
        }

        private void textBoxName_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxName.Text))
            {
                e.Cancel = true;
                errorProvider1.SetError(textBoxName, "You must provide a name");
            }
        }

        private void comboBoxProjects_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(comboBoxProjects.Text))
            {
                e.Cancel = true;
                errorProvider1.SetError(comboBoxProjects, "You must select the target project");
            }
        }

        private void textBoxOutput_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxOutput.Text))
            {
                e.Cancel = true;
                errorProvider1.SetError(textBoxOutput, "You must provide the path for generated file (relative to the project path)");
            }
        }

        private void textBoxTemplate_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxTemplate.Text))
            {
                e.Cancel = true;
                errorProvider1.SetError(textBoxTemplate, "You must provide path to the template file (relative to the scaffolding config file)");
            }
        }

        private void buttonFindTemplate_Click(object sender, EventArgs e)
        {
            var rootDir = Path.GetDirectoryName(_cfg.FileName);
            openFileDialog1.InitialDirectory = rootDir;
            if (!string.IsNullOrEmpty(textBoxTemplate.Text))
            {
                var file = Path.Combine(rootDir, textBoxTemplate.Text);
                if (File.Exists(file))
                {
                    openFileDialog1.InitialDirectory = Path.GetDirectoryName(file);
                    openFileDialog1.FileName = file;
                }
            }
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                var filepath = openFileDialog1.FileName;
                if (filepath.StartsWith(rootDir, StringComparison.OrdinalIgnoreCase))
                {
                    filepath = filepath.Substring(rootDir.Length + 1);
                }
                textBoxTemplate.Text = filepath;
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (this.ValidateChildren()) 
                DialogResult = DialogResult.OK;
        }
    }
}
